var curr_page = 1;
var per_page = 10;
var num_pages = Math.ceil(searchresults.length / per_page);
if(num_pages == 0){
	num_pages = 1;
}



function makePaginator(){
	$("#paginator").paginate({
		count: num_pages,
		start: curr_page,
		display: 5,
		border: true,
		border_color: '#CD9F33',
		text_color: '#6F4632',
		background_color: '#FAF2E1',	
		border_hover_color: '#B18110',
		text_hover_color: '#6F4632',
		background_hover_color: '#DCC388', 
		images: false,
		mouse: 'press',
		onChange: function(page){
			DisplayPage(page);
			positionPaginator();
			replaceIdByName();
			replaceIdByPic();
		}
	});
}

function DisplayPage(page){			
	var html = '';
	num = 0;
	curr_page = page;
	html += '<ul class="wants-list">';
	for (var i = 0; i < per_page; i++){
		var counter = ((page - 1) * per_page) + i;
		
		if (typeof(searchresults[counter]) == "undefined"){
			if(html == '<ul class="wants-list">'){
				html += '<br /><span class="medium-text">No wants found. <br /><br />Do you have anything that you want?</span>';
			}
			break;
		}
		
		html += '<li>';
		
		html += '<div class="single-want normal-text">';
		html += '<div style="float: left width=80% margin: 10px; padding: 5px;"><a href="profile/' + searchresults[counter].fb_id+ '"><span class="get-fb-name">'+searchresults[counter].fb_id+'</span></a><span class="small-text"> ('+categories[searchresults[counter].category_id]+')</span>: ' + searchresults[counter].description.substr(0, 350); + '</div>';
		if(current_user == searchresults[counter].fb_id){
			html += '<div id="delete-button" style="float:right; margin-right: 5%"><a href="Javascript:void(0);" onclick = "deletewant('+searchresults[counter].want_id+')" ></a></div>';
		}
		else{
			html += '<div id="help-button" style="float:right; margin-right: 5%"><a href="Javascript:void(0);" onclick = "help('+searchresults[counter].want_id+')" ></a></div>';
		}
		html += '</div>';
		html += '<div style="clear:both"></div>';
		html += '</li>';
	}
	html += '</ul>';
	$("#wants-results").html(html);
}


function positionPaginator(){
	var width = $('#wants-container').width();
	var length = num_pages > 5 ? 5 : num_pages;
	var actual = 60 + 60 + 25 * num_pages;
	$('#paginator').css('margin-left', (width - actual) / 2);
}


$(function(){
	makePaginator();
	DisplayPage(curr_page);
	positionPaginator();
});